package com.tecode.myBooks.user.servlet;

import com.learn.thymeleaf3.config.TemplateEngineUtil;
import com.tecode.myBooks.user.bean.User;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2018-12-07
 * @Description:
 */
@WebServlet("/user/list")
public class TestServlet1 extends HttpServlet {


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(req.getServletContext());
		WebContext context = new WebContext(req, resp, req.getServletContext());


		List<User> userList = new ArrayList<User>();
		for (int i = 10; i <20; i++) {
			userList.add(new User("张三"+i,"中国广州"+i));
		}

		//req.setAttribute("userList",userList);
		//req.getRequestDispatcher("/TestThymeleaf.html").forward(req,resp);

		resp.setCharacterEncoding("utf-8");
		context.setVariable("users", userList);
		engine.process("TestThymeleaf.html", context, resp.getWriter());
	}
}